package com.zzx.test.question.mi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author ZhouZhiXin
 * @ClassName ZhouClass1
 * @Description Java文本编辑题自测
 * @Date 2020/12/24
 * @Time 11:10
 */
public class ZhouClass1 {

    public static void main(String[] args) {
        long currentTimeMillis = System.currentTimeMillis();
        String str = "how are you i am fine thanks";
        System.out.println("计算文本的长度:" + textSize(str));
        System.out.println("计算文本中单词的数量:" + howManyWords(str));
        System.out.println("计算文本中最常出现的字母以及它的出现次数:" + mostUsedLetter(str));
        System.out.println("将文本中单词按字典排序:" + sortWords(str));
        System.out.println("去掉文本中所有a开头的单词:" + filterWords(str));
        System.out.println("用时:" + (System.currentTimeMillis() - currentTimeMillis) + "ms");
    }

    public static int textSize(String string) {
        return string.length();
    }

    public static int howManyWords(String string) {
        return string.split(String.valueOf((char) 32)).length;
    }

    public static int mostUsedLetter(String string) {
        int res = 0;
        int[] ints = new int[26];
        string = string.replace(" ", "");
        for (char c : string.toCharArray()) {
            ints[c - 'a']++;
        }
        for (int i : ints) {
            if (i > res) {
                res = i;
            }
        }
        return res;
    }

    public static String sortWords(String string) {
        String[] strings = string.split(" ");
        Arrays.sort(strings);
        return String.join(" ", strings);
    }


    public static String filterWords(String string) {
        List<String> list = new ArrayList<>();
        for (String s : string.split(" ")) {
            if (s.toCharArray()[0] != 'a') {
                list.add(s);
            }
        }
        return String.join(" ", list);
    }

}
